package com.woniuxy.microgoodsserver.attributemag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.AttributeInfo;
import com.woniuxy.microcommonserver.bean.gy.BrandInfo;
import com.woniuxy.microcommonserver.bean.gy.ValueInfo;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcommonserver.pageHelper.TableResult;
import com.woniuxy.microcommonserver.res.ResultMsg;
import com.woniuxy.microgoodsserver.attributemag.service.AttributeService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**属性和其值接口
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/23 14:36
 */
@RequestMapping("attribute")
@RestController
public class AttributeController {
    @Resource
    private AttributeService attributeServiceImpl;
    /**
        增添规格名称和值
     * @params:
     * @return
     */
    @PostMapping(value = "/addAttribute",produces = MediaType.APPLICATION_JSON_VALUE)
    public String addAttribute(AttributeInfo attributeInfo,@RequestParam(value ="valueInfos",required = true) List<String> valueInfos){
        System.out.println(attributeInfo);
        System.out.println(valueInfos);
        attributeServiceImpl.addAttribute(attributeInfo,valueInfos);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("添加成功").build());
    }
    /**
        查询所有的规格
     * @params:
     * @return
     */
    @GetMapping(value = "/findAllAttribute",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult findAllAttribute(PageRequest pageRequest){
        PageInfo<AttributeInfo> allAttribute = attributeServiceImpl.findAllAttribute(pageRequest.getPageNum(),pageRequest.getLimit());
        return new TableResult(allAttribute.getTotal(),allAttribute.getList());
    }


    /**
        删除规格名称
     * @params:
     * @return
     */
    @DeleteMapping(value = "/deleteAttribute",produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteAttribute(AttributeInfo attributeInfo){
        attributeServiceImpl.deleteAttribute(attributeInfo);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("删除成功").build());
    }


    /**
        根据ID查询需要修改规格和其属性值
     * @params:
     * @return
     */
    @GetMapping(value = "/findAttributeById",produces = MediaType.APPLICATION_JSON_VALUE)
    public String findAttributeById(AttributeInfo attributeInfo){
        List<Map> map = attributeServiceImpl.findAttributeById(attributeInfo);
        return JSONObject.toJSONString(map);
    }

    /**
        删除属性操作
     * @params:
     * @return
     */
    @DeleteMapping(value = "/deleteValue",produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteValue(Integer vid){
        attributeServiceImpl.deleteValue(vid);

        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("删除成功").build());
    }

    /**
        修改对应的规格和其属性
     * @params:
     * @return
     */
    @PutMapping(value = "/updateAttribute",produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateAttribute(AttributeInfo attributeInfo,@RequestParam(value ="valueInfos",required = true) List<String> valueInfos){
        attributeServiceImpl.updateAttribute(attributeInfo,valueInfos);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("修改成功").build());
    }


    /**
        根据分类ID查询规格名称和其规格属性值,用于发布商品详情页规格与商品的匹配
     * @params:
     * @return
     */
    @PutMapping(value = "/findAttributeAndValue",produces = MediaType.APPLICATION_JSON_VALUE)
    public String findAttributeAndValue(Integer sortId){
        List<Map> maps = attributeServiceImpl.findAttributeAndValue(sortId);
        return JSONObject.toJSONString(maps);
    }
}
